package com.imooc.mall.controller;

import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.filter.UserFilter;
import com.imooc.mall.model.vo.CartVO;
import com.imooc.mall.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zwj
 * @Description: 购物车模块
 * @date 2022/12/3 4:38 PM
 */
@Api(tags = "购物车模块")
@RestController
@RequestMapping(value = "/cart")
public class CartController {
    @Resource
    private CartService cartService;

    @ApiOperation("添加商品到购物车")
    @PostMapping(value = "/add")
    public ApiRestResponse<List<CartVO>> add(@RequestParam Integer count, @RequestParam Integer productId) {
        List<CartVO> result = cartService.add(productId, count);
        return ApiRestResponse.success(result);
    }

    @ApiOperation("购物车列表")
    @GetMapping(value = "/list")
    public ApiRestResponse<List<CartVO>> list() {
        List<CartVO> result = cartService.list(UserFilter.currentUser.getId());
        return ApiRestResponse.success(result);
    }

    @ApiOperation("更新购物车某个商品的数量")
    @PostMapping(value = "/update")
    public ApiRestResponse<List<CartVO>> update(@RequestParam Integer productId, @RequestParam Integer count) {
        List<CartVO> result = cartService.update(UserFilter.currentUser.getId(), productId, count);
        return ApiRestResponse.success(result);
    }

    @ApiOperation("删除购物车的某个商品")
    @PostMapping(value = "/delete")
    public ApiRestResponse<List<CartVO>> delete(@RequestParam Integer productId) {
        List<CartVO> result = cartService.delete(UserFilter.currentUser.getId(), productId);
        return ApiRestResponse.success(result);
    }

    @ApiOperation("选中/不选中购物车的某个商品")
    @PostMapping(value = "/select")
    public ApiRestResponse<List<CartVO>> select(@RequestParam Integer productId, @RequestParam Integer selected) {
        List<CartVO> result = cartService.selectOrNot(UserFilter.currentUser.getId(), productId, selected);
        return ApiRestResponse.success(result);
    }

    @ApiOperation("全选/全不选购物车的某个商品")
    @PostMapping(value = "/selectAll")
    public ApiRestResponse<List<CartVO>> selectAll(@RequestParam Integer selected) {
        List<CartVO> result = cartService.selectOrNotAll(UserFilter.currentUser.getId(), selected);
        return ApiRestResponse.success(result);
    }
}
